<?php

namespace Model;

/**
 * Class Page
 * @property-read \Lib\Functions $fns
 * @package Model
 */
class Upload extends Model {
    const SAVE_DIR = 'uploads';
    
    public function saveImage($file) {
        $data = $file;
die;
        if(empty($this->_key)) {
            throw new \Exception('文件上传空的key');
        }

        if(empty($data)) {
            throw new \Exception('缺少上传的文件: ' . $this->_key);
        }

        if($data['error']) {
            throw new \Exception('文件上传失败');
        }

        if($data['size'] == 0) {
            throw new \Exception('空文件');
        }

        if(empty($data['tmp_name'])) {
            throw new \Exception('目标保存文件丢失');
        }

        $dirname = dirname($fullPath);

        if(!is_dir($dirname)) {
            mkdir($dirname, 0777, true);
        }

        $tmp = $data['tmp_name'];
        $uploadDir = dirname($fullPath);

        if(!is_dir($uploadDir)) {
            throw new \Exception('文件上传保存失败(目录创建失败)');
        } else if (!is_writable($uploadDir)) {
            throw new \Exception('文件上传保存失败(没有写入权限)');
        } else if(!move_uploaded_file($tmp, $fullPath)) {
            throw new \Exception('文件上传保存失败(未知错误)');
        }

        return $fullPath;
    }
    
    public function getTemplatePath($template) {
        $pageDir = $this->core->rootPath('core/pages');
        return "$pageDir/$template.phtml";
    }
    
    public function getTemplateInfo($template) {
        $templateFile = $this->getTemplatePath($template);
        
        if(!file_exists($templateFile)) {
            return [];
        }
        
        $_get_template_info = true;
        $data = include($templateFile);
        unset($_get_template_info);
        return $data;
    }
    
    public function exists($page) {
        $pageDirPath = $this->core->storagePath(self::PAGE_SAVE_DIR);
        $file = "$pageDirPath/$page.page.json";

        return file_exists($file);
    }
    
    public function save($page, $data) {
        $pageDirPath = $this->core->storagePath(self::PAGE_SAVE_DIR);
        $file = "$pageDirPath/$page.page.json";
        
        if(!is_dir($pageDirPath)) {
            mkdir($pageDirPath, 0777, true);
        }
        
        file_put_contents($file, json_encode($data));
    }
    
    public function remove($page) {
        $pageDirPath = $this->core->storagePath(self::PAGE_SAVE_DIR);
        $file = "$pageDirPath/$page.page.json";
        
        if(file_exists($file)) {
            unlink($file);
        }
    }
    
    public function getTemplates() {
        $pageDir = $this->core->rootPath('core/pages');

        if(!file_exists($pageDir)) {
            return [];
        }

        $templateFiles = glob($pageDir . '/*.phtml');

        if(empty($templateFiles)) {
            return [];
        }

        $templates = [];
        $_get_template_info = true;

        foreach ($templateFiles as $file) {
            $info = include $file;
            $templates []= $info;
        }

        unset($_get_template_info);
        return $templates;
    }
    
    public function getPages() {
        $pageDirPath = $this->core->storagePath(self::PAGE_SAVE_DIR);
        $pages = glob("$pageDirPath/*.page.json");

        $pageInfos = [];

        foreach ($pages as $pageFile) {
            $id = preg_replace('#\.page\.json$#i', '', basename($pageFile));
            $pageInfos []= $this->getInfo($id);
        }

        return $pageInfos;
    }
}